/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "gimiutils.h"

namespace gimi
{

std::string getGimiErrorString(const int errorNo)
{
  std::string errorString;
  
  switch (errorNo) {
    case GIMI_OK:
      errorString = "ok";
      break;
    case GIMI_ERROR:
      errorString = "error";
      break;
    case GIMI_TIMEOUT:
      errorString = "timed out";
      break;
    case GIMI_NOCONNECTION:
      errorString = "no connection";
      break;
    case GIMI_INTERRUPT:
      errorString = "interrupted";
      break;
    case GIMI_NOSUBSCRIBERS:
      errorString = "no subscribers";
      break;
    case GIMI_INVALIDTYPE:
      errorString = "invalid type";
      break;
    case GIMI_NOMSGS:
      errorString = "no messages";
      break;
    case GIMI_INVALIDCLIENT:
      errorString = "invalid client";
      break;
    case GIMI_EMPTY:
      errorString = "empty";
      break;
    case GIMI_INVALIDNAME:
      errorString = "invalid name";
      break;
    case GIMI_TCPSTACK:
      errorString = "tcp-stack error";
      break;
    case GIMI_CREATESOCKETERROR:
      errorString = "error creating socket";
      break;
    case GIMI_RESOLVEHOSTERROR:
      errorString = "error resolving host";
      break;
    case GIMI_CONNECTIONERROR:
      errorString = "connection error";
      break;
    case GIMI_SOCKETREADERROR:
      errorString = "could not read socket";
      break;
    case GIMI_INVALIDHUBDATA:
      errorString = "invalid hub packet";
      break;
    default:
      errorString = "nonspecified error";
      break;
  }
  
  return errorString;
}

GIMIService::GIMIService()
  : clientId (0),
    clientName (),
    serviceType (0),
    majorServiceId (0),
    minorServiceId (0),
    serviceDescription (),
    serviceLocation (),
    gimiVersion (),
    clientVersion ()
{}

GIMIService::GIMIService(const int cId,
                         const std::string cName,
                         const int sType,
                         const int majorSId,
                         const int minorSId,
                         const std::string sDescr /* = "" */,
                         const std::string sLoc /* = "" */,
                         const int gVersion /* = 0 */,
                         const int gBuild /* = 0 */,
                         const int cVersion /* = 0 */,
                         const int cBuild /* = 0 */)
  : clientId (cId),
    clientName (cName),
    serviceType (sType),
    majorServiceId (majorSId),
    minorServiceId (minorSId),
    serviceDescription (sDescr),
    serviceLocation (sLoc),
    gimiVersion (gVersion, gBuild),
    clientVersion (cVersion, cBuild)
{}

std::string GIMIService::getShortServiceTypeString() const
{
  std::string shortService;
  switch (serviceType) {
    case GIMI_SERVICETYPE_PROVIDED:
      shortService = "P";
      break;
    case GIMI_SERVICETYPE_DESCRIPTION:
      shortService = "D";
      break;
    case GIMI_SERVICETYPE_ACCEPTED:
      shortService = "A";
      break;
    default:
      shortService = "?";
      break;
  }
  return shortService;
}

std::string GIMIService::getServiceTypeString() const
{
  std::string serviceString;
  switch (serviceType) {
    case GIMI_SERVICETYPE_PROVIDED:
      serviceString = "Provided";
      break;
    case GIMI_SERVICETYPE_DESCRIPTION:
      serviceString = "Description";
      break;
    case GIMI_SERVICETYPE_ACCEPTED:
      serviceString = "Accepted";
      break;
    default:
      serviceString = "?";
      break;
  }
  return serviceString;
}

GIMIRedirection::GIMIRedirection(const unsigned int targetQId,
                                 const int majorSId /* = -1 */,
                                 const int minorSId /* = -1 */,
                                 const int sendId /* = -1 */)
  : targetQueueId (targetQId),
    majorServiceId (majorSId),
    minorServiceId (minorSId),
    senderId (sendId)
{}

} // end namespace gimi
